﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLNhaSach.BUS;
using QLNhaSach.BUS.DTO;

namespace QLNhaSach.GUI.NhanVienQuanLySach
{
    public partial class SoLuongSachTon : Form
    {
        DataTable dt = new DataTable();
        public SoLuongSachTon()
        {
            InitializeComponent();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SoLuongSachTon_Load(object sender, EventArgs e)
        {
            dt = NhanVienBanSachBUS.DSSach();
            lvDSSach.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ListViewItem item = new ListViewItem();
                item.Text = dr["MaSach"].ToString();
                item.SubItems.Add(dr["TenSach"].ToString());
                item.SubItems.Add(dr["SLDau"].ToString());
                item.SubItems.Add(dr["SLNhap"].ToString());
                item.SubItems.Add(dr["SLBan"].ToString());
                item.SubItems.Add(dr["SLCuoi"].ToString());
                lvDSSach.Items.Add(item);
            }

            cbNXB.Items.Clear();
            cbTheLoai.Items.Clear();

            DataTable dtTL = new DataTable();
            dtTL = NhanVienQuanLySachBUS.DSTheLoai();
            for (int i = 0; i < dtTL.Rows.Count; i++)
            {
                DataRow dr = dtTL.Rows[i];
                cbTheLoai.Items.Add(dr["TenTL"]);
            }

            DataTable dtNXB = new DataTable();
            dtNXB = NhanVienQuanLySachBUS.DSNXB();
            for (int i = 0; i < dtNXB.Rows.Count; i++)
            {
                DataRow dr = dtNXB.Rows[i];
                cbNXB.Items.Add(dr["TenNXB"]);
            }     
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            if (txtTenSach.Text == "")
            {
                MessageBox.Show("Tên sách không được trống");
                txtTenSach.Focus();
                return;
            }

            string sql = "SELECT * FROM Sach WHERE (TenSach LIKE '%" + txtTenSach.Text + "%') ";
            if (txtMaSach.Text != "")
            {
                long temp;
                if (long.TryParse(txtMaSach.Text, out temp) == false)
                {
                    MessageBox.Show("Mã sách là kiểu số");
                    txtMaSach.Text = "";
                    txtMaSach.Focus();
                    return;
                }
                sql += " AND (MaSach = " + Convert.ToInt16(txtMaSach.Text) + ")";
            }
            if (cbTheLoai.SelectedIndex != -1)
            {
                int temp = cbTheLoai.SelectedIndex + 1;
                sql += " AND (MaTL = " + temp + ")";
            }
            if (cbNXB.SelectedIndex != -1)
            {
                int temp = cbNXB.SelectedIndex + 1;
                sql += " AND (MaNXB = " + temp + ")";
            }
            DataTable table = new DataTable();
            table = NhanVienBanSachBUS.TimSach(sql);
            lvDSSach.Items.Clear();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow dr = table.Rows[i];
                ListViewItem item = new ListViewItem();
                item.Text = dr["MaSach"].ToString();
                item.SubItems.Add(dr["TenSach"].ToString());
                item.SubItems.Add(dr["SLDau"].ToString());
                item.SubItems.Add(dr["SLNhap"].ToString());
                item.SubItems.Add(dr["SLBan"].ToString());
                item.SubItems.Add(dr["SLCuoi"].ToString());
                lvDSSach.Items.Add(item);
            }
        }
    }
}